
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>使用 Redis 如何设计分布式锁？使用 Zookeeper 来设计分布式锁可以吗？以上两种分布式锁的实现方式哪种效率比较高？ · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="zookeeper-application-scenarios.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">高并发架构</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../high-concurrent/mq-interview.html">
            
                <a href="../high-concurrent/mq-interview.html">
            
                    
                    消息队列
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="../high-concurrent/why-mq.html">
            
                <a href="../high-concurrent/why-mq.html">
            
                    
                    为什么使用消息队列？消息队列有什么优点和缺点？Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么优点和缺点？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="../high-concurrent/how-to-ensure-high-availability-of-message-queues.html">
            
                <a href="../high-concurrent/how-to-ensure-high-availability-of-message-queues.html">
            
                    
                    如何保证消息队列的高可用？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.3" data-path="../high-concurrent/how-to-ensure-that-messages-are-not-repeatedly-consumed.html">
            
                <a href="../high-concurrent/how-to-ensure-that-messages-are-not-repeatedly-consumed.html">
            
                    
                    如何保证消息不被重复消费？（如何保证消息消费的幂等性）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.4" data-path="../high-concurrent/how-to-ensure-the-reliable-transmission-of-messages.html">
            
                <a href="../high-concurrent/how-to-ensure-the-reliable-transmission-of-messages.html">
            
                    
                    如何保证消息的可靠性传输？（如何处理消息丢失的问题）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.5" data-path="../high-concurrent/how-to-ensure-the-order-of-messages.html">
            
                <a href="../high-concurrent/how-to-ensure-the-order-of-messages.html">
            
                    
                    如何保证消息的顺序性？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.6" data-path="../high-concurrent/mq-time-delay-and-expired-failure.html">
            
                <a href="../high-concurrent/mq-time-delay-and-expired-failure.html">
            
                    
                    如何解决消息队列的延时以及过期失效问题？消息队列满了以后该怎么处理？有几百万消息持续积压几小时，说说怎么解决？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.1.7" data-path="../high-concurrent/mq-design.html">
            
                <a href="../high-concurrent/mq-design.html">
            
                    
                    如果让你写一个消息队列，该如何进行架构设计啊？说一下你的思路。
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../high-concurrent/es-introduction.html">
            
                <a href="../high-concurrent/es-introduction.html">
            
                    
                    搜索引擎
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.2.1" data-path="../high-concurrent/es-architecture.html">
            
                <a href="../high-concurrent/es-architecture.html">
            
                    
                    es 的分布式架构原理能说一下么（es 是如何实现分布式的啊）？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.2" data-path="../high-concurrent/es-write-query-search.html">
            
                <a href="../high-concurrent/es-write-query-search.html">
            
                    
                    es 写入数据的工作原理是什么啊？es 查询数据的工作原理是什么啊？底层的 lucene 介绍一下呗？倒排索引了解吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.3" data-path="../high-concurrent/es-optimizing-query-performance.html">
            
                <a href="../high-concurrent/es-optimizing-query-performance.html">
            
                    
                    es 在数据量很大的情况下（数十亿级别）如何提高查询效率啊？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2.4" data-path="../high-concurrent/es-production-cluster.html">
            
                <a href="../high-concurrent/es-production-cluster.html">
            
                    
                    es 生产集群的部署架构是什么？每个索引的数据量大概有多少？每个索引大概有多少个分片？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.3" >
            
                <span>
            
                    
                    缓存
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.3.1" data-path="../high-concurrent/why-cache.html">
            
                <a href="../high-concurrent/why-cache.html">
            
                    
                    在项目中缓存是如何使用的？缓存如果使用不当会造成什么后果？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.2" data-path="../high-concurrent/redis-single-thread-model.html">
            
                <a href="../high-concurrent/redis-single-thread-model.html">
            
                    
                    Redis 和 Memcached 有什么区别？Redis 的线程模型是什么？为什么单线程的 Redis 比多线程的 Memcached 效率要高得多？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.3" data-path="../high-concurrent/redis-data-types.html">
            
                <a href="../high-concurrent/redis-data-types.html">
            
                    
                    Redis 都有哪些数据类型？分别在哪些场景下使用比较合适？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.4" data-path="../high-concurrent/redis-expiration-policies-and-lru.html">
            
                <a href="../high-concurrent/redis-expiration-policies-and-lru.html">
            
                    
                    Redis 的过期策略都有哪些？手写一下 LRU 代码实现？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.5" data-path="../high-concurrent/how-to-ensure-high-concurrent-and-high-availability-of-redis.html">
            
                <a href="../high-concurrent/how-to-ensure-high-concurrent-and-high-availability-of-redis.html">
            
                    
                    如何保证 Redis 高并发、高可用？Redis 的主从复制原理能介绍一下么？Redis 的哨兵原理能介绍一下么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.6" data-path="../high-concurrent/redis-persistence.html">
            
                <a href="../high-concurrent/redis-persistence.html">
            
                    
                    Redis 的持久化有哪几种方式？不同的持久化机制都有什么优缺点？持久化机制具体底层是如何实现的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.7" data-path="../high-concurrent/redis-cluster.html">
            
                <a href="../high-concurrent/redis-cluster.html">
            
                    
                    Redis 集群模式的工作原理能说一下么？在集群模式下，Redis 的 key 是如何寻址的？分布式寻址都有哪些算法？了解一致性 hash 算法吗？如何动态增加和删除一个节点？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.8" data-path="../high-concurrent/redis-caching-avalanche-and-caching-penetration.html">
            
                <a href="../high-concurrent/redis-caching-avalanche-and-caching-penetration.html">
            
                    
                    了解什么是 redis 的雪崩、穿透和击穿？Redis 崩溃之后会怎么样？系统该如何应对这种情况？如何处理 Redis 的穿透？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.9" data-path="../high-concurrent/redis-consistence.html">
            
                <a href="../high-concurrent/redis-consistence.html">
            
                    
                    如何保证缓存与数据库的双写一致性？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.10" data-path="../high-concurrent/redis-cas.html">
            
                <a href="../high-concurrent/redis-cas.html">
            
                    
                    Redis 的并发竞争问题是什么？如何解决这个问题？了解 Redis 事务的 CAS 方案吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3.11" data-path="../high-concurrent/redis-production-environment.html">
            
                <a href="../high-concurrent/redis-production-environment.html">
            
                    
                    生产环境中的 Redis 是怎么部署的？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.4" >
            
                <span>
            
                    
                    分库分表
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.4.1" data-path="../high-concurrent/database-shard.html">
            
                <a href="../high-concurrent/database-shard.html">
            
                    
                    为什么要分库分表（设计高并发系统的时候，数据库层面该如何设计）？用过哪些分库分表中间件？不同的分库分表中间件都有什么优点和缺点？你们具体是如何对数据库如何进行垂直拆分或水平拆分的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.2" data-path="../high-concurrent/database-shard-method.html">
            
                <a href="../high-concurrent/database-shard-method.html">
            
                    
                    现在有一个未分库分表的系统，未来要分库分表，如何设计才可以让系统从未分库分表动态切换到分库分表上？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.3" data-path="../high-concurrent/database-shard-dynamic-expand.html">
            
                <a href="../high-concurrent/database-shard-dynamic-expand.html">
            
                    
                    如何设计可以动态扩容缩容的分库分表方案？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4.4" data-path="../high-concurrent/database-shard-global-id-generate.html">
            
                <a href="../high-concurrent/database-shard-global-id-generate.html">
            
                    
                    分库分表之后，id 主键如何处理？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.5" >
            
                <span>
            
                    
                    读写分离
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.5.1" data-path="../high-concurrent/mysql-read-write-separation.html">
            
                <a href="../high-concurrent/mysql-read-write-separation.html">
            
                    
                    如何实现 MySQL 的读写分离？MySQL 主从复制原理是啥？如何解决 MySQL 主从同步的延时问题？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.6" >
            
                <span>
            
                    
                    高并发系统
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.6.1" data-path="../high-concurrent/high-concurrent-design.html">
            
                <a href="../high-concurrent/high-concurrent-design.html">
            
                    
                    如何设计一个高并发系统？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">分布式系统</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="distributed-system-interview.html">
            
                <a href="distributed-system-interview.html">
            
                    
                    面试连环炮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" >
            
                <span>
            
                    
                    系统拆分
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.2.1" data-path="why-dubbo.html">
            
                <a href="why-dubbo.html">
            
                    
                    为什么要进行系统拆分？如何进行系统拆分？拆分后不用 Dubbo 可以吗？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.3" >
            
                <span>
            
                    
                    分布式服务框架
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="dubbo-operating-principle.html">
            
                <a href="dubbo-operating-principle.html">
            
                    
                    说一下 Dubbo 的工作原理？注册中心挂了可以继续通信吗？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="dubbo-serialization-protocol.html">
            
                <a href="dubbo-serialization-protocol.html">
            
                    
                    Dubbo 支持哪些序列化协议？说一下 Hessian 的数据结构？PB 知道吗？为什么 PB 的效率是最高的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="dubbo-load-balancing.html">
            
                <a href="dubbo-load-balancing.html">
            
                    
                    Dubbo 负载均衡策略和集群容错策略都有哪些？动态代理策略呢？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.4" data-path="dubbo-spi.html">
            
                <a href="dubbo-spi.html">
            
                    
                    Dubbo 的 spi 思想是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.5" data-path="dubbo-service-management.html">
            
                <a href="dubbo-service-management.html">
            
                    
                    如何基于 Dubbo 进行服务治理、服务降级、失败重试以及超时重试？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.6" data-path="distributed-system-idempotency.html">
            
                <a href="distributed-system-idempotency.html">
            
                    
                    分布式服务接口的幂等性如何设计（比如不能重复扣款）？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.7" data-path="distributed-system-request-sequence.html">
            
                <a href="distributed-system-request-sequence.html">
            
                    
                    分布式服务接口请求的顺序性如何保证？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3.8" data-path="dubbo-rpc-design.html">
            
                <a href="dubbo-rpc-design.html">
            
                    
                    如何自己设计一个类似 Dubbo 的 RPC 框架？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" >
            
                <span>
            
                    
                    分布式锁
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="zookeeper-application-scenarios.html">
            
                <a href="zookeeper-application-scenarios.html">
            
                    
                    Zookeeper 都有哪些应用场景？
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="3.4.2" data-path="distributed-lock-redis-vs-zookeeper.html">
            
                <a href="distributed-lock-redis-vs-zookeeper.html">
            
                    
                    使用 Redis 如何设计分布式锁？使用 Zookeeper 来设计分布式锁可以吗？以上两种分布式锁的实现方式哪种效率比较高？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" >
            
                <span>
            
                    
                    分布式事务
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.5.1" data-path="distributed-transaction.html">
            
                <a href="distributed-transaction.html">
            
                    
                    分布式事务了解吗？你们如何解决分布式事务问题的？TCC 如果出现网络连不通怎么办？XA 的一致性如何保证？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.6" >
            
                <span>
            
                    
                    分布式会话
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.6.1" data-path="distributed-session.html">
            
                <a href="distributed-session.html">
            
                    
                    集群部署时的分布式 Session 如何实现？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="../high-availability/hystrix-introduction.html">
            
                <a href="../high-availability/hystrix-introduction.html">
            
                    
                    Hystrix 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.8" data-path="../high-availability/e-commerce-website-detail-page-architecture.html">
            
                <a href="../high-availability/e-commerce-website-detail-page-architecture.html">
            
                    
                    电商网站详情页系统架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.9" data-path="../high-availability/hystrix-thread-pool-isolation.html">
            
                <a href="../high-availability/hystrix-thread-pool-isolation.html">
            
                    
                    Hystrix 线程池技术实现资源隔离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.10" data-path="../high-availability/hystrix-semphore-isolation.html">
            
                <a href="../high-availability/hystrix-semphore-isolation.html">
            
                    
                    Hystrix 信号量机制实现资源隔离
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.11" data-path="../high-availability/hystrix-execution-isolation.html">
            
                <a href="../high-availability/hystrix-execution-isolation.html">
            
                    
                    Hystrix 隔离策略细粒度控制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.12" data-path="../high-availability/hystrix-process.html">
            
                <a href="../high-availability/hystrix-process.html">
            
                    
                    深入 Hystrix 执行时内部原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.13" data-path="../high-availability/hystrix-request-cache.html">
            
                <a href="../high-availability/hystrix-request-cache.html">
            
                    
                    基于 request cache 请求缓存技术优化批量商品数据查询接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.14" data-path="../high-availability/hystrix-fallback.html">
            
                <a href="../high-availability/hystrix-fallback.html">
            
                    
                    基于本地缓存的 fallback 降级机制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.15" data-path="../high-availability/hystrix-circuit-breaker.html">
            
                <a href="../high-availability/hystrix-circuit-breaker.html">
            
                    
                    深入 Hystrix 断路器执行原理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.16" data-path="../high-availability/hystrix-thread-pool-current-limiting.html">
            
                <a href="../high-availability/hystrix-thread-pool-current-limiting.html">
            
                    
                    深入 Hystrix 线程池隔离与接口限流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.17" data-path="../high-availability/hystrix-timeout.html">
            
                <a href="../high-availability/hystrix-timeout.html">
            
                    
                    基于 timeout 机制为服务接口调用超时提供安全保护
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.18" >
            
                <span>
            
                    
                    高可用系统
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.18.1" >
            
                <span>
            
                    
                    如何设计一个高可用系统？
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.19" >
            
                <span>
            
                    
                    限流
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.19.1" data-path="../high-concurrent/huifer-how-to-limit-current.html">
            
                <a href="../high-concurrent/huifer-how-to-limit-current.html">
            
                    
                    如何限流？在工作中是怎么做的？说一下具体的实现？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.20" >
            
                <span>
            
                    
                    熔断
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.20.1" >
            
                <span>
            
                    
                    如何进行熔断？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="3.20.2" >
            
                <span>
            
                    
                    熔断框架都有哪些？具体实现原理知道吗？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="3.20.3" data-path="../high-availability/sentinel-vs-hystrix.html">
            
                <a href="../high-availability/sentinel-vs-hystrix.html">
            
                    
                    熔断框架如何做技术选型？选用 Sentinel 还是 Hystrix？
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.21" >
            
                <span>
            
                    
                    降级
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.21.1" >
            
                <span>
            
                    
                    如何进行降级？
            
                </span>
            

            
        </li>
    

            </ul>
            
        </li>
    

    
        
        <li class="header">微服务架构</li>
        
        
    
        <li class="chapter " data-level="4.1" >
            
                <a target="_blank" href="https://github.com/doocs/advanced-java">
            
                    
                    微服务架构整个章节内容属额外新增，后续抽空更新，也欢迎读者们参与补充完善
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../micro-services/microservices-introduction.html">
            
                <a href="../micro-services/microservices-introduction.html">
            
                    
                    关于微服务架构的描述
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../micro-services/migrating-from-a-monolithic-architecture-to-a-microservices-architecture.html">
            
                <a href="../micro-services/migrating-from-a-monolithic-architecture-to-a-microservices-architecture.html">
            
                    
                    从单体式架构迁移到微服务架构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../micro-services/event-driven-data-management-for-microservices.html">
            
                <a href="../micro-services/event-driven-data-management-for-microservices.html">
            
                    
                    微服务的事件驱动数据管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../micro-services/choose-microservice-deployment-strategy.html">
            
                <a href="../micro-services/choose-microservice-deployment-strategy.html">
            
                    
                    选择微服务部署策略
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">Spring Cloud 微服务架构</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../micro-services/huifer-what's-microservice-how-to-communicate.html">
            
                <a href="../micro-services/huifer-what's-microservice-how-to-communicate.html">
            
                    
                    什么是微服务？微服务之间是如何独立通讯的？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" >
            
                <span>
            
                    
                    Spring Cloud 和 Dubbo 有哪些区别？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" >
            
                <span>
            
                    
                    Spring Boot 和 Spring Cloud，谈谈你对它们的理解？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" >
            
                <span>
            
                    
                    什么是服务熔断？什么是服务降级？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" >
            
                <span>
            
                    
                    微服务的优缺点分别是什么？说一下你在项目开发中碰到的坑？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../micro-services/huifer-micro-services-technology-stack.html">
            
                <a href="../micro-services/huifer-micro-services-technology-stack.html">
            
                    
                    你所知道的微服务技术栈都有哪些？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="../micro-services/huifer-micro-service-governance.html">
            
                <a href="../micro-services/huifer-micro-service-governance.html">
            
                    
                    微服务治理策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.8" >
            
                <span>
            
                    
                    Eureka 和 Zookeeper 都可以提供服务注册与发现的功能，它们有什么区别？
            
                </span>
            

            
        </li>
    
        <li class="chapter " data-level="5.9" >
            
                <span>
            
                    
                    ......
            
                </span>
            

            
        </li>
    

    
        
        <li class="header">海量数据处理</li>
        
        
    
        <li class="chapter " data-level="6.1" data-path="../big-data/find-common-urls.html">
            
                <a href="../big-data/find-common-urls.html">
            
                    
                    如何从大量的 URL 中找出相同的 URL？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="../big-data/find-top-100-words.html">
            
                <a href="../big-data/find-top-100-words.html">
            
                    
                    如何从大量数据中找出高频词？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="../big-data/find-top-1-ip.html">
            
                <a href="../big-data/find-top-1-ip.html">
            
                    
                    如何找出某一天访问百度网站最多的 IP？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="../big-data/find-no-repeat-number.html">
            
                <a href="../big-data/find-no-repeat-number.html">
            
                    
                    如何在大量的数据中找出不重复的整数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="../big-data/find-a-number-if-exists.html">
            
                <a href="../big-data/find-a-number-if-exists.html">
            
                    
                    如何在大量的数据中判断一个数是否存在？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="../big-data/find-hotest-query-string.html">
            
                <a href="../big-data/find-hotest-query-string.html">
            
                    
                    如何查询最热门的查询串？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.7" data-path="../big-data/count-different-phone-numbers.html">
            
                <a href="../big-data/count-different-phone-numbers.html">
            
                    
                    如何统计不同电话号码的个数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.8" data-path="../big-data/find-mid-value-in-500-millions.html">
            
                <a href="../big-data/find-mid-value-in-500-millions.html">
            
                    
                    如何从 5 亿个数中找出中位数？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.9" data-path="../big-data/sort-the-query-strings-by-counts.html">
            
                <a href="../big-data/sort-the-query-strings-by-counts.html">
            
                    
                    如何按照 query 的频度排序？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="6.10" data-path="../big-data/find-rank-top-500-numbers.html">
            
                <a href="../big-data/find-rank-top-500-numbers.html">
            
                    
                    如何找出排名前 500 的数？
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >使用 Redis 如何设计分布式锁？使用 Zookeeper 来设计分布式锁可以吗？以上两种分布式锁的实现方式哪种效率比较高？</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x9762;&#x8BD5;&#x9898;">&#x9762;&#x8BD5;&#x9898;</h2>
<p>&#x4E00;&#x822C;&#x5B9E;&#x73B0;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x90FD;&#x6709;&#x54EA;&#x4E9B;&#x65B9;&#x5F0F;&#xFF1F;&#x4F7F;&#x7528; redis &#x5982;&#x4F55;&#x8BBE;&#x8BA1;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#xFF1F;&#x4F7F;&#x7528; zk &#x6765;&#x8BBE;&#x8BA1;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x53EF;&#x4EE5;&#x5417;&#xFF1F;&#x8FD9;&#x4E24;&#x79CD;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#x54EA;&#x79CD;&#x6548;&#x7387;&#x6BD4;&#x8F83;&#x9AD8;&#xFF1F;</p>
<h2 id="&#x9762;&#x8BD5;&#x5B98;&#x5FC3;&#x7406;&#x5206;&#x6790;">&#x9762;&#x8BD5;&#x5B98;&#x5FC3;&#x7406;&#x5206;&#x6790;</h2>
<p>&#x5176;&#x5B9E;&#x4E00;&#x822C;&#x95EE;&#x95EE;&#x9898;&#xFF0C;&#x90FD;&#x662F;&#x8FD9;&#x4E48;&#x95EE;&#x7684;&#xFF0C;&#x5148;&#x95EE;&#x95EE;&#x4F60; zk&#xFF0C;&#x7136;&#x540E;&#x5176;&#x5B9E;&#x662F;&#x8981;&#x8FC7;&#x6E21;&#x5230; zk &#x76F8;&#x5173;&#x7684;&#x4E00;&#x4E9B;&#x95EE;&#x9898;&#x91CC;&#x53BB;&#xFF0C;&#x6BD4;&#x5982;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x3002;&#x56E0;&#x4E3A;&#x5728;&#x5206;&#x5E03;&#x5F0F;&#x7CFB;&#x7EDF;&#x5F00;&#x53D1;&#x4E2D;&#xFF0C;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7684;&#x4F7F;&#x7528;&#x573A;&#x666F;&#x8FD8;&#x662F;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x3002;</p>
<h2 id="&#x9762;&#x8BD5;&#x9898;&#x5256;&#x6790;">&#x9762;&#x8BD5;&#x9898;&#x5256;&#x6790;</h2>
<h3 id="redis-&#x5206;&#x5E03;&#x5F0F;&#x9501;">redis &#x5206;&#x5E03;&#x5F0F;&#x9501;</h3>
<p>&#x5B98;&#x65B9;&#x53EB;&#x505A; <code>RedLock</code> &#x7B97;&#x6CD5;&#xFF0C;&#x662F; redis &#x5B98;&#x65B9;&#x652F;&#x6301;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7B97;&#x6CD5;&#x3002;</p>
<p>&#x8FD9;&#x4E2A;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x6709; 3 &#x4E2A;&#x91CD;&#x8981;&#x7684;&#x8003;&#x91CF;&#x70B9;&#xFF1A;</p>
<ul>
<li>&#x4E92;&#x65A5;&#xFF08;&#x53EA;&#x80FD;&#x6709;&#x4E00;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x83B7;&#x53D6;&#x9501;&#xFF09;</li>
<li>&#x4E0D;&#x80FD;&#x6B7B;&#x9501;</li>
<li>&#x5BB9;&#x9519;&#xFF08;&#x53EA;&#x8981;&#x5927;&#x90E8;&#x5206; redis &#x8282;&#x70B9;&#x521B;&#x5EFA;&#x4E86;&#x8FD9;&#x628A;&#x9501;&#x5C31;&#x53EF;&#x4EE5;&#xFF09;</li>
</ul>
<h4 id="redis-&#x6700;&#x666E;&#x901A;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;">redis &#x6700;&#x666E;&#x901A;&#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;</h4>
<p>&#x7B2C;&#x4E00;&#x4E2A;&#x6700;&#x666E;&#x901A;&#x7684;&#x5B9E;&#x73B0;&#x65B9;&#x5F0F;&#xFF0C;&#x5C31;&#x662F;&#x5728; redis &#x91CC;&#x4F7F;&#x7528; <code>setnx</code> &#x547D;&#x4EE4;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; key&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x7B97;&#x52A0;&#x9501;&#x3002;</p>
<pre><code class="lang-r">SET resource_name my_random_value NX PX <span class="hljs-number">30000</span>
</code></pre>
<p>&#x6267;&#x884C;&#x8FD9;&#x4E2A;&#x547D;&#x4EE4;&#x5C31; ok&#x3002;</p>
<ul>
<li><code>NX</code>&#xFF1A;&#x8868;&#x793A;&#x53EA;&#x6709; <code>key</code> &#x4E0D;&#x5B58;&#x5728;&#x7684;&#x65F6;&#x5019;&#x624D;&#x4F1A;&#x8BBE;&#x7F6E;&#x6210;&#x529F;&#x3002;&#xFF08;&#x5982;&#x679C;&#x6B64;&#x65F6; redis &#x4E2D;&#x5B58;&#x5728;&#x8FD9;&#x4E2A; key&#xFF0C;&#x90A3;&#x4E48;&#x8BBE;&#x7F6E;&#x5931;&#x8D25;&#xFF0C;&#x8FD4;&#x56DE; <code>nil</code>&#xFF09;</li>
<li><code>PX 30000</code>&#xFF1A;&#x610F;&#x601D;&#x662F; 30s &#x540E;&#x9501;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x3002;&#x522B;&#x4EBA;&#x521B;&#x5EFA;&#x7684;&#x65F6;&#x5019;&#x5982;&#x679C;&#x53D1;&#x73B0;&#x5DF2;&#x7ECF;&#x6709;&#x4E86;&#x5C31;&#x4E0D;&#x80FD;&#x52A0;&#x9501;&#x4E86;&#x3002;</li>
</ul>
<p>&#x91CA;&#x653E;&#x9501;&#x5C31;&#x662F;&#x5220;&#x9664; key &#xFF0C;&#x4F46;&#x662F;&#x4E00;&#x822C;&#x53EF;&#x4EE5;&#x7528; <code>lua</code> &#x811A;&#x672C;&#x5220;&#x9664;&#xFF0C;&#x5224;&#x65AD; value &#x4E00;&#x6837;&#x624D;&#x5220;&#x9664;&#xFF1A;</p>
<pre><code class="lang-lua"><span class="hljs-comment">-- &#x5220;&#x9664;&#x9501;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x627E;&#x5230; key &#x5BF9;&#x5E94;&#x7684; value&#xFF0C;&#x8DDF;&#x81EA;&#x5DF1;&#x4F20;&#x8FC7;&#x53BB;&#x7684; value &#x505A;&#x6BD4;&#x8F83;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x4E00;&#x6837;&#x7684;&#x624D;&#x5220;&#x9664;&#x3002;</span>
<span class="hljs-keyword">if</span> redis.call(<span class="hljs-string">&quot;get&quot;</span>,KEYS[<span class="hljs-number">1</span>]) == ARGV[<span class="hljs-number">1</span>] <span class="hljs-keyword">then</span>
    <span class="hljs-keyword">return</span> redis.call(<span class="hljs-string">&quot;del&quot;</span>,KEYS[<span class="hljs-number">1</span>])
<span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>&#x4E3A;&#x5565;&#x8981;&#x7528; <code>random_value</code> &#x968F;&#x673A;&#x503C;&#x5462;&#xFF1F;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x67D0;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x83B7;&#x53D6;&#x5230;&#x4E86;&#x9501;&#xFF0C;&#x4F46;&#x662F;&#x963B;&#x585E;&#x4E86;&#x5F88;&#x957F;&#x65F6;&#x95F4;&#x624D;&#x6267;&#x884C;&#x5B8C;&#xFF0C;&#x6BD4;&#x5982;&#x8BF4;&#x8D85;&#x8FC7;&#x4E86; 30s&#xFF0C;&#x6B64;&#x65F6;&#x53EF;&#x80FD;&#x5DF2;&#x7ECF;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x9501;&#x4E86;&#xFF0C;&#x6B64;&#x65F6;&#x53EF;&#x80FD;&#x522B;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x83B7;&#x53D6;&#x5230;&#x4E86;&#x8FD9;&#x4E2A;&#x9501;&#xFF0C;&#x8981;&#x662F;&#x4F60;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x76F4;&#x63A5;&#x5220;&#x9664; key &#x7684;&#x8BDD;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#xFF0C;&#x6240;&#x4EE5;&#x5F97;&#x7528;&#x968F;&#x673A;&#x503C;&#x52A0;&#x4E0A;&#x9762;&#x7684; <code>lua</code> &#x811A;&#x672C;&#x6765;&#x91CA;&#x653E;&#x9501;&#x3002;</p>
<p>&#x4F46;&#x662F;&#x8FD9;&#x6837;&#x662F;&#x80AF;&#x5B9A;&#x4E0D;&#x884C;&#x7684;&#x3002;&#x56E0;&#x4E3A;&#x5982;&#x679C;&#x662F;&#x666E;&#x901A;&#x7684; redis &#x5355;&#x5B9E;&#x4F8B;&#xFF0C;&#x90A3;&#x5C31;&#x662F;&#x5355;&#x70B9;&#x6545;&#x969C;&#x3002;&#x6216;&#x8005;&#x662F; redis &#x666E;&#x901A;&#x4E3B;&#x4ECE;&#xFF0C;&#x90A3; redis &#x4E3B;&#x4ECE;&#x5F02;&#x6B65;&#x590D;&#x5236;&#xFF0C;&#x5982;&#x679C;&#x4E3B;&#x8282;&#x70B9;&#x6302;&#x4E86;&#xFF08;key &#x5C31;&#x6CA1;&#x6709;&#x4E86;&#xFF09;&#xFF0C;key &#x8FD8;&#x6CA1;&#x540C;&#x6B65;&#x5230;&#x4ECE;&#x8282;&#x70B9;&#xFF0C;&#x6B64;&#x65F6;&#x4ECE;&#x8282;&#x70B9;&#x5207;&#x6362;&#x4E3A;&#x4E3B;&#x8282;&#x70B9;&#xFF0C;&#x522B;&#x4EBA;&#x5C31;&#x53EF;&#x4EE5; set key&#xFF0C;&#x4ECE;&#x800C;&#x62FF;&#x5230;&#x9501;&#x3002;</p>
<h4 id="redlock-&#x7B97;&#x6CD5;">RedLock &#x7B97;&#x6CD5;</h4>
<p>&#x8FD9;&#x4E2A;&#x573A;&#x666F;&#x662F;&#x5047;&#x8BBE;&#x6709;&#x4E00;&#x4E2A; redis cluster&#xFF0C;&#x6709; 5 &#x4E2A; redis master &#x5B9E;&#x4F8B;&#x3002;&#x7136;&#x540E;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x6B65;&#x9AA4;&#x83B7;&#x53D6;&#x4E00;&#x628A;&#x9501;&#xFF1A;</p>
<ol>
<li>&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x6233;&#xFF0C;&#x5355;&#x4F4D;&#x662F;&#x6BEB;&#x79D2;&#xFF1B;</li>
<li>&#x8DDF;&#x4E0A;&#x9762;&#x7C7B;&#x4F3C;&#xFF0C;&#x8F6E;&#x6D41;&#x5C1D;&#x8BD5;&#x5728;&#x6BCF;&#x4E2A; master &#x8282;&#x70B9;&#x4E0A;&#x521B;&#x5EFA;&#x9501;&#xFF0C;&#x8FC7;&#x671F;&#x65F6;&#x95F4;&#x8F83;&#x77ED;&#xFF0C;&#x4E00;&#x822C;&#x5C31;&#x51E0;&#x5341;&#x6BEB;&#x79D2;&#xFF1B;</li>
<li>&#x5C1D;&#x8BD5;&#x5728;<strong>&#x5927;&#x591A;&#x6570;&#x8282;&#x70B9;</strong>&#x4E0A;&#x5EFA;&#x7ACB;&#x4E00;&#x4E2A;&#x9501;&#xFF0C;&#x6BD4;&#x5982; 5 &#x4E2A;&#x8282;&#x70B9;&#x5C31;&#x8981;&#x6C42;&#x662F; 3 &#x4E2A;&#x8282;&#x70B9; <code>n / 2 + 1</code>&#xFF1B;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x8BA1;&#x7B97;&#x5EFA;&#x7ACB;&#x597D;&#x9501;&#x7684;&#x65F6;&#x95F4;&#xFF0C;&#x5982;&#x679C;&#x5EFA;&#x7ACB;&#x9501;&#x7684;&#x65F6;&#x95F4;&#x5C0F;&#x4E8E;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#xFF0C;&#x5C31;&#x7B97;&#x5EFA;&#x7ACB;&#x6210;&#x529F;&#x4E86;&#xFF1B;</li>
<li>&#x8981;&#x662F;&#x9501;&#x5EFA;&#x7ACB;&#x5931;&#x8D25;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x4F9D;&#x6B21;&#x4E4B;&#x524D;&#x5EFA;&#x7ACB;&#x8FC7;&#x7684;&#x9501;&#x5220;&#x9664;&#xFF1B;</li>
<li>&#x53EA;&#x8981;&#x522B;&#x4EBA;&#x5EFA;&#x7ACB;&#x4E86;&#x4E00;&#x628A;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#xFF0C;&#x4F60;&#x5C31;&#x5F97;<strong>&#x4E0D;&#x65AD;&#x8F6E;&#x8BE2;&#x53BB;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;</strong>&#x3002;</li>
</ol>
<p><img src="../images/redis-redlock.png" alt="redis-redlock"></p>
<p><a href="https://redis.io/" target="_blank">Redis &#x5B98;&#x65B9;</a>&#x7ED9;&#x51FA;&#x4E86;&#x4EE5;&#x4E0A;&#x4E24;&#x79CD;&#x57FA;&#x4E8E; Redis &#x5B9E;&#x73B0;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#xFF1A;<a href="https://redis.io/topics/distlock" target="_blank">https://redis.io/topics/distlock</a> &#x3002;</p>
<h3 id="zk-&#x5206;&#x5E03;&#x5F0F;&#x9501;">zk &#x5206;&#x5E03;&#x5F0F;&#x9501;</h3>
<p>zk &#x5206;&#x5E03;&#x5F0F;&#x9501;&#xFF0C;&#x5176;&#x5B9E;&#x53EF;&#x4EE5;&#x505A;&#x7684;&#x6BD4;&#x8F83;&#x7B80;&#x5355;&#xFF0C;&#x5C31;&#x662F;&#x67D0;&#x4E2A;&#x8282;&#x70B9;&#x5C1D;&#x8BD5;&#x521B;&#x5EFA;&#x4E34;&#x65F6; znode&#xFF0C;&#x6B64;&#x65F6;&#x521B;&#x5EFA;&#x6210;&#x529F;&#x4E86;&#x5C31;&#x83B7;&#x53D6;&#x4E86;&#x8FD9;&#x4E2A;&#x9501;&#xFF1B;&#x8FD9;&#x4E2A;&#x65F6;&#x5019;&#x522B;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x6765;&#x521B;&#x5EFA;&#x9501;&#x4F1A;&#x5931;&#x8D25;&#xFF0C;&#x53EA;&#x80FD;<strong>&#x6CE8;&#x518C;&#x4E2A;&#x76D1;&#x542C;&#x5668;</strong>&#x76D1;&#x542C;&#x8FD9;&#x4E2A;&#x9501;&#x3002;&#x91CA;&#x653E;&#x9501;&#x5C31;&#x662F;&#x5220;&#x9664;&#x8FD9;&#x4E2A; znode&#xFF0C;&#x4E00;&#x65E6;&#x91CA;&#x653E;&#x6389;&#x5C31;&#x4F1A;&#x901A;&#x77E5;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x7136;&#x540E;&#x6709;&#x4E00;&#x4E2A;&#x7B49;&#x5F85;&#x7740;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5C31;&#x53EF;&#x4EE5;&#x518D;&#x6B21;&#x91CD;&#x65B0;&#x52A0;&#x9501;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * ZooKeeperSession
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ZooKeeperSession</span> </span>{

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> CountDownLatch connectedSemaphore = <span class="hljs-keyword">new</span> CountDownLatch(<span class="hljs-number">1</span>);

    <span class="hljs-keyword">private</span> ZooKeeper zookeeper;
    <span class="hljs-keyword">private</span> CountDownLatch latch;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ZooKeeperSession</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">this</span>.zookeeper = <span class="hljs-keyword">new</span> ZooKeeper(<span class="hljs-string">&quot;192.168.31.187:2181,192.168.31.19:2181,192.168.31.227:2181&quot;</span>, <span class="hljs-number">50000</span>, <span class="hljs-keyword">new</span> ZooKeeperWatcher());
            <span class="hljs-keyword">try</span> {
                connectedSemaphore.await();
            } <span class="hljs-keyword">catch</span> (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(<span class="hljs-string">&quot;ZooKeeper session established......&quot;</span>);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }

    <span class="hljs-comment">/**
     * &#x83B7;&#x53D6;&#x5206;&#x5E03;&#x5F0F;&#x9501;
     * 
     * <span class="hljs-doctag">@param</span> productId
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Boolean <span class="hljs-title">acquireDistributedLock</span><span class="hljs-params">(Long productId)</span> </span>{
        String path = <span class="hljs-string">&quot;/product-lock-&quot;</span> + productId;

        <span class="hljs-keyword">try</span> {
            zookeeper.create(path, <span class="hljs-string">&quot;&quot;</span>.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
            <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
        } <span class="hljs-keyword">catch</span> (Exception e) {
            <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>) {
                <span class="hljs-keyword">try</span> {
                    <span class="hljs-comment">// &#x76F8;&#x5F53;&#x4E8E;&#x662F;&#x7ED9;node&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#xFF0C;&#x53BB;&#x770B;&#x770B;&#x8FD9;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#x662F;&#x5426;&#x5B58;&#x5728;</span>
                    Stat stat = zk.exists(path, <span class="hljs-keyword">true</span>);

                    <span class="hljs-keyword">if</span> (stat != <span class="hljs-keyword">null</span>) {
                        <span class="hljs-keyword">this</span>.latch = <span class="hljs-keyword">new</span> CountDownLatch(<span class="hljs-number">1</span>);
                        <span class="hljs-keyword">this</span>.latch.await(waitTime, TimeUnit.MILLISECONDS);
                        <span class="hljs-keyword">this</span>.latch = <span class="hljs-keyword">null</span>;
                    }
                    zookeeper.create(path, <span class="hljs-string">&quot;&quot;</span>.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                    <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
                } <span class="hljs-keyword">catch</span> (Exception ee) {
                    <span class="hljs-keyword">continue</span>;
                }
            }

        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }

    <span class="hljs-comment">/**
     * &#x91CA;&#x653E;&#x6389;&#x4E00;&#x4E2A;&#x5206;&#x5E03;&#x5F0F;&#x9501;
     * 
     * <span class="hljs-doctag">@param</span> productId
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">releaseDistributedLock</span><span class="hljs-params">(Long productId)</span> </span>{
        String path = <span class="hljs-string">&quot;/product-lock-&quot;</span> + productId;
        <span class="hljs-keyword">try</span> {
            zookeeper.delete(path, -<span class="hljs-number">1</span>);
            System.out.println(<span class="hljs-string">&quot;release the lock for product[id=&quot;</span> + productId + <span class="hljs-string">&quot;]......&quot;</span>);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            e.printStackTrace();
        }
    }

    <span class="hljs-comment">/**
     * &#x5EFA;&#x7ACB; zk session &#x7684; watcher
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ZooKeeperWatcher</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Watcher</span> </span>{

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(WatchedEvent event)</span> </span>{
            System.out.println(<span class="hljs-string">&quot;Receive watched event: &quot;</span> + event.getState());

            <span class="hljs-keyword">if</span> (KeeperState.SyncConnected == event.getState()) {
                connectedSemaphore.countDown();
            }

            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.latch != <span class="hljs-keyword">null</span>) {
                <span class="hljs-keyword">this</span>.latch.countDown();
            }
        }

    }

    <span class="hljs-comment">/**
     * &#x5C01;&#x88C5;&#x5355;&#x4F8B;&#x7684;&#x9759;&#x6001;&#x5185;&#x90E8;&#x7C7B;
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Singleton</span> </span>{

        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ZooKeeperSession instance;

        <span class="hljs-keyword">static</span> {
            instance = <span class="hljs-keyword">new</span> ZooKeeperSession();
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ZooKeeperSession <span class="hljs-title">getInstance</span><span class="hljs-params">()</span> </span>{
            <span class="hljs-keyword">return</span> instance;
        }

    }

    <span class="hljs-comment">/**
     * &#x83B7;&#x53D6;&#x5355;&#x4F8B;
     * 
     * <span class="hljs-doctag">@return</span>
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> ZooKeeperSession <span class="hljs-title">getInstance</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">return</span> Singleton.getInstance();
    }

    <span class="hljs-comment">/**
     * &#x521D;&#x59CB;&#x5316;&#x5355;&#x4F8B;&#x7684;&#x4FBF;&#x6377;&#x65B9;&#x6CD5;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>{
        getInstance();
    }

}
</code></pre>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x91C7;&#x7528;&#x53E6;&#x4E00;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x521B;&#x5EFA;&#x4E34;&#x65F6;&#x987A;&#x5E8F;&#x8282;&#x70B9;&#xFF1A;</p>
<p>&#x5982;&#x679C;&#x6709;&#x4E00;&#x628A;&#x9501;&#xFF0C;&#x88AB;&#x591A;&#x4E2A;&#x4EBA;&#x7ED9;&#x7ADE;&#x4E89;&#xFF0C;&#x6B64;&#x65F6;&#x591A;&#x4E2A;&#x4EBA;&#x4F1A;&#x6392;&#x961F;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x62FF;&#x5230;&#x9501;&#x7684;&#x4EBA;&#x4F1A;&#x6267;&#x884C;&#xFF0C;&#x7136;&#x540E;&#x91CA;&#x653E;&#x9501;&#xFF1B;&#x540E;&#x9762;&#x7684;&#x6BCF;&#x4E2A;&#x4EBA;&#x90FD;&#x4F1A;&#x53BB;&#x76D1;&#x542C;<strong>&#x6392;&#x5728;&#x81EA;&#x5DF1;&#x524D;&#x9762;</strong>&#x7684;&#x90A3;&#x4E2A;&#x4EBA;&#x521B;&#x5EFA;&#x7684; node &#x4E0A;&#xFF0C;&#x4E00;&#x65E6;&#x67D0;&#x4E2A;&#x4EBA;&#x91CA;&#x653E;&#x4E86;&#x9501;&#xFF0C;&#x6392;&#x5728;&#x81EA;&#x5DF1;&#x540E;&#x9762;&#x7684;&#x4EBA;&#x5C31;&#x4F1A;&#x88AB; zookeeper &#x7ED9;&#x901A;&#x77E5;&#xFF0C;&#x4E00;&#x65E6;&#x88AB;&#x901A;&#x77E5;&#x4E86;&#x4E4B;&#x540E;&#xFF0C;&#x5C31; ok &#x4E86;&#xFF0C;&#x81EA;&#x5DF1;&#x5C31;&#x83B7;&#x53D6;&#x5230;&#x4E86;&#x9501;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x6267;&#x884C;&#x4EE3;&#x7801;&#x4E86;&#x3002;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ZooKeeperDistributedLock</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Watcher</span> </span>{

    <span class="hljs-keyword">private</span> ZooKeeper zk;
    <span class="hljs-keyword">private</span> String locksRoot = <span class="hljs-string">&quot;/locks&quot;</span>;
    <span class="hljs-keyword">private</span> String productId;
    <span class="hljs-keyword">private</span> String waitNode;
    <span class="hljs-keyword">private</span> String lockNode;
    <span class="hljs-keyword">private</span> CountDownLatch latch;
    <span class="hljs-keyword">private</span> CountDownLatch connectedLatch = <span class="hljs-keyword">new</span> CountDownLatch(<span class="hljs-number">1</span>);
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> sessionTimeout = <span class="hljs-number">30000</span>;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ZooKeeperDistributedLock</span><span class="hljs-params">(String productId)</span> </span>{
        <span class="hljs-keyword">this</span>.productId = productId;
        <span class="hljs-keyword">try</span> {
            String address = <span class="hljs-string">&quot;192.168.31.187:2181,192.168.31.19:2181,192.168.31.227:2181&quot;</span>;
            zk = <span class="hljs-keyword">new</span> ZooKeeper(address, sessionTimeout, <span class="hljs-keyword">this</span>);
            connectedLatch.await();
        } <span class="hljs-keyword">catch</span> (IOException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        } <span class="hljs-keyword">catch</span> (KeeperException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        } <span class="hljs-keyword">catch</span> (InterruptedException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">process</span><span class="hljs-params">(WatchedEvent event)</span> </span>{
        <span class="hljs-keyword">if</span> (event.getState() == KeeperState.SyncConnected) {
            connectedLatch.countDown();
            <span class="hljs-keyword">return</span>;
        }

        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.latch != <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">this</span>.latch.countDown();
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">acquireDistributedLock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">try</span> {
            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.tryLock()) {
                <span class="hljs-keyword">return</span>;
            } <span class="hljs-keyword">else</span> {
                waitForLock(waitNode, sessionTimeout);
            }
        } <span class="hljs-keyword">catch</span> (KeeperException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        } <span class="hljs-keyword">catch</span> (InterruptedException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">tryLock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">try</span> {
             <span class="hljs-comment">// &#x4F20;&#x5165;&#x8FDB;&#x53BB;&#x7684;locksRoot + &#x201C;/&#x201D; + productId</span>
            <span class="hljs-comment">// &#x5047;&#x8BBE;productId&#x4EE3;&#x8868;&#x4E86;&#x4E00;&#x4E2A;&#x5546;&#x54C1;id&#xFF0C;&#x6BD4;&#x5982;&#x8BF4;1</span>
            <span class="hljs-comment">// locksRoot = locks</span>
            <span class="hljs-comment">// /locks/10000000000&#xFF0C;/locks/10000000001&#xFF0C;/locks/10000000002</span>
            lockNode = zk.create(locksRoot + <span class="hljs-string">&quot;/&quot;</span> + productId, <span class="hljs-keyword">new</span> <span class="hljs-keyword">byte</span>[<span class="hljs-number">0</span>], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

            <span class="hljs-comment">// &#x770B;&#x770B;&#x521A;&#x521B;&#x5EFA;&#x7684;&#x8282;&#x70B9;&#x662F;&#x4E0D;&#x662F;&#x6700;&#x5C0F;&#x7684;&#x8282;&#x70B9;</span>
             <span class="hljs-comment">// locks&#xFF1A;10000000000&#xFF0C;10000000001&#xFF0C;10000000002</span>
            List&lt;String&gt; locks = zk.getChildren(locksRoot, <span class="hljs-keyword">false</span>);
            Collections.sort(locks);

            <span class="hljs-keyword">if</span>(lockNode.equals(locksRoot+<span class="hljs-string">&quot;/&quot;</span>+ locks.get(<span class="hljs-number">0</span>))){
                <span class="hljs-comment">//&#x5982;&#x679C;&#x662F;&#x6700;&#x5C0F;&#x7684;&#x8282;&#x70B9;,&#x5219;&#x8868;&#x793A;&#x53D6;&#x5F97;&#x9501;</span>
                <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
            }

            <span class="hljs-comment">//&#x5982;&#x679C;&#x4E0D;&#x662F;&#x6700;&#x5C0F;&#x7684;&#x8282;&#x70B9;&#xFF0C;&#x627E;&#x5230;&#x6BD4;&#x81EA;&#x5DF1;&#x5C0F;1&#x7684;&#x8282;&#x70B9;</span>
      <span class="hljs-keyword">int</span> previousLockIndex = -<span class="hljs-number">1</span>;
            <span class="hljs-keyword">for</span>(<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; locks.size(); i++) {
        <span class="hljs-keyword">if</span>(lockNode.equals(locksRoot + &#x201C;/&#x201D; + locks.get(i))) {
                     previousLockIndex = i - <span class="hljs-number">1</span>;
            <span class="hljs-keyword">break</span>;
        }
       }

       <span class="hljs-keyword">this</span>.waitNode = locks.get(previousLockIndex);
        } <span class="hljs-keyword">catch</span> (KeeperException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        } <span class="hljs-keyword">catch</span> (InterruptedException e) {
            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> LockException(e);
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">false</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">waitForLock</span><span class="hljs-params">(String waitNode, <span class="hljs-keyword">long</span> waitTime)</span> <span class="hljs-keyword">throws</span> InterruptedException, KeeperException </span>{
        Stat stat = zk.exists(locksRoot + <span class="hljs-string">&quot;/&quot;</span> + waitNode, <span class="hljs-keyword">true</span>);
        <span class="hljs-keyword">if</span> (stat != <span class="hljs-keyword">null</span>) {
            <span class="hljs-keyword">this</span>.latch = <span class="hljs-keyword">new</span> CountDownLatch(<span class="hljs-number">1</span>);
            <span class="hljs-keyword">this</span>.latch.await(waitTime, TimeUnit.MILLISECONDS);
            <span class="hljs-keyword">this</span>.latch = <span class="hljs-keyword">null</span>;
        }
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">unlock</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">try</span> {
            <span class="hljs-comment">// &#x5220;&#x9664;/locks/10000000000&#x8282;&#x70B9;</span>
            <span class="hljs-comment">// &#x5220;&#x9664;/locks/10000000001&#x8282;&#x70B9;</span>
            System.out.println(<span class="hljs-string">&quot;unlock &quot;</span> + lockNode);
            zk.delete(lockNode, -<span class="hljs-number">1</span>);
            lockNode = <span class="hljs-keyword">null</span>;
            zk.close();
        } <span class="hljs-keyword">catch</span> (InterruptedException e) {
            e.printStackTrace();
        } <span class="hljs-keyword">catch</span> (KeeperException e) {
            e.printStackTrace();
        }
    }

    <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">LockException</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">RuntimeException</span> </span>{
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">long</span> serialVersionUID = <span class="hljs-number">1L</span>;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LockException</span><span class="hljs-params">(String e)</span> </span>{
            <span class="hljs-keyword">super</span>(e);
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LockException</span><span class="hljs-params">(Exception e)</span> </span>{
            <span class="hljs-keyword">super</span>(e);
        }
    }
}
</code></pre>
<h3 id="redis-&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x548C;-zk-&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7684;&#x5BF9;&#x6BD4;">redis &#x5206;&#x5E03;&#x5F0F;&#x9501;&#x548C; zk &#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7684;&#x5BF9;&#x6BD4;</h3>
<ul>
<li>redis &#x5206;&#x5E03;&#x5F0F;&#x9501;&#xFF0C;&#x5176;&#x5B9E;<strong>&#x9700;&#x8981;&#x81EA;&#x5DF1;&#x4E0D;&#x65AD;&#x53BB;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;</strong>&#xFF0C;&#x6BD4;&#x8F83;&#x6D88;&#x8017;&#x6027;&#x80FD;&#x3002;</li>
<li>zk &#x5206;&#x5E03;&#x5F0F;&#x9501;&#xFF0C;&#x83B7;&#x53D6;&#x4E0D;&#x5230;&#x9501;&#xFF0C;&#x6CE8;&#x518C;&#x4E2A;&#x76D1;&#x542C;&#x5668;&#x5373;&#x53EF;&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x4E0D;&#x65AD;&#x4E3B;&#x52A8;&#x5C1D;&#x8BD5;&#x83B7;&#x53D6;&#x9501;&#xFF0C;&#x6027;&#x80FD;&#x5F00;&#x9500;&#x8F83;&#x5C0F;&#x3002;</li>
</ul>
<p>&#x53E6;&#x5916;&#x4E00;&#x70B9;&#x5C31;&#x662F;&#xFF0C;&#x5982;&#x679C;&#x662F; redis &#x83B7;&#x53D6;&#x9501;&#x7684;&#x90A3;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF; &#x51FA;&#x73B0; bug &#x6302;&#x4E86;&#xFF0C;&#x90A3;&#x4E48;&#x53EA;&#x80FD;&#x7B49;&#x5F85;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x4E4B;&#x540E;&#x624D;&#x80FD;&#x91CA;&#x653E;&#x9501;&#xFF1B;&#x800C; zk &#x7684;&#x8BDD;&#xFF0C;&#x56E0;&#x4E3A;&#x521B;&#x5EFA;&#x7684;&#x662F;&#x4E34;&#x65F6; znode&#xFF0C;&#x53EA;&#x8981;&#x5BA2;&#x6237;&#x7AEF;&#x6302;&#x4E86;&#xFF0C;znode &#x5C31;&#x6CA1;&#x4E86;&#xFF0C;&#x6B64;&#x65F6;&#x5C31;&#x81EA;&#x52A8;&#x91CA;&#x653E;&#x9501;&#x3002;</p>
<p>redis &#x5206;&#x5E03;&#x5F0F;&#x9501;&#x5927;&#x5BB6;&#x6CA1;&#x53D1;&#x73B0;&#x597D;&#x9EBB;&#x70E6;&#x5417;&#xFF1F;&#x904D;&#x5386;&#x4E0A;&#x9501;&#xFF0C;&#x8BA1;&#x7B97;&#x65F6;&#x95F4;&#x7B49;&#x7B49;......zk &#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x8BED;&#x4E49;&#x6E05;&#x6670;&#x5B9E;&#x73B0;&#x7B80;&#x5355;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x5148;&#x4E0D;&#x5206;&#x6790;&#x592A;&#x591A;&#x7684;&#x4E1C;&#x897F;&#xFF0C;&#x5C31;&#x8BF4;&#x8FD9;&#x4E24;&#x70B9;&#xFF0C;&#x6211;&#x4E2A;&#x4EBA;&#x5B9E;&#x8DF5;&#x8BA4;&#x4E3A; zk &#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x6BD4; redis &#x7684;&#x5206;&#x5E03;&#x5F0F;&#x9501;&#x7262;&#x9760;&#x3001;&#x800C;&#x4E14;&#x6A21;&#x578B;&#x7B80;&#x5355;&#x6613;&#x7528;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="zookeeper-application-scenarios.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: Zookeeper 都有哪些应用场景？">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"使用 Redis 如何设计分布式锁？使用 Zookeeper 来设计分布式锁可以吗？以上两种分布式锁的实现方式哪种效率比较高？","level":"3.4.2","depth":2,"next":{"title":"分布式事务","level":"3.5","depth":1,"ref":"","articles":[{"title":"分布式事务了解吗？你们如何解决分布式事务问题的？TCC 如果出现网络连不通怎么办？XA 的一致性如何保证？","level":"3.5.1","depth":2,"path":"distributed-system/distributed-transaction.md","ref":"distributed-system/distributed-transaction.md","articles":[]}]},"previous":{"title":"Zookeeper 都有哪些应用场景？","level":"3.4.1","depth":2,"path":"distributed-system/zookeeper-application-scenarios.md","ref":"distributed-system/zookeeper-application-scenarios.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"distributed-system/distributed-lock-redis-vs-zookeeper.md","mtime":"2019-12-30T01:36:31.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-01-08T08:38:14.143Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

